rm update
[EroBeats.git] / Djinn and Tonic - Erobeats / iowin32.c
blob68fc8d19a526dd6363e6d73899fa29c06de089d9
1 /* iowin32.c -- IO base function header for compress/uncompress .zip
2 files using zlib + zip or unzip API
3 This IO API version uses the Win32 API (for Microsoft Windows)
5 Version 1.01e, February 12th, 2005
7 Copyright (C) 1998-2005 Gilles Vollant
8 */
10 #include <stdlib.h>
12 #include "ZLIB/zlib.h"
13 #include "ioapi.h"
14 #include "iowin32.h"
16 #ifndef INVALID_HANDLE_VALUE
17 #define INVALID_HANDLE_VALUE (0xFFFFFFFF)
18 #endif
20 #ifndef INVALID_SET_FILE_POINTER
21 #define INVALID_SET_FILE_POINTER ((DWORD)-1)
22 #endif
24 voidpf ZCALLBACK win32_open_file_func OF((
25 voidpf opaque,
26 const char* filename,
27 int mode));
29 uLong ZCALLBACK win32_read_file_func OF((
30 voidpf opaque,
31 voidpf stream,
32 void* buf,
33 uLong size));
35 uLong ZCALLBACK win32_write_file_func OF((
36 voidpf opaque,
37 voidpf stream,
38 const void* buf,
39 uLong size));
41 long ZCALLBACK win32_tell_file_func OF((
42 voidpf opaque,
43 voidpf stream));
45 long ZCALLBACK win32_seek_file_func OF((
46 voidpf opaque,
47 voidpf stream,
48 uLong offset,
49 int origin));
51 int ZCALLBACK win32_close_file_func OF((
52 voidpf opaque,
53 voidpf stream));
55 int ZCALLBACK win32_error_file_func OF((
56 voidpf opaque,
57 voidpf stream));
59 typedef struct
61 HANDLE hf;
62 int error;
63 } WIN32FILE_IOWIN;
65 voidpf ZCALLBACK win32_open_file_func (opaque, filename, mode)
66 voidpf opaque;
67 const char* filename;
68 int mode;
70 const char* mode_fopen = NULL;
71 DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
72 HANDLE hFile = 0;
73 voidpf ret=NULL;
75 dwDesiredAccess = dwShareMode = dwFlagsAndAttributes = 0;
77 if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
79 dwDesiredAccess = GENERIC_READ;
80 dwCreationDisposition = OPEN_EXISTING;
81 dwShareMode = FILE_SHARE_READ;
83 else
84 if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
86 dwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
87 dwCreationDisposition = OPEN_EXISTING;
89 else
90 if (mode & ZLIB_FILEFUNC_MODE_CREATE)
92 dwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
93 dwCreationDisposition = CREATE_ALWAYS;
96 if ((filename!=NULL) && (dwDesiredAccess != 0))
97 hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL,
98 dwCreationDisposition, dwFlagsAndAttributes, NULL);
100 if (hFile == INVALID_HANDLE_VALUE)
101 hFile = NULL;
103 if (hFile != NULL)
105 WIN32FILE_IOWIN w32fiow;
106 w32fiow.hf = hFile;
107 w32fiow.error = 0;
108 ret = malloc(sizeof(WIN32FILE_IOWIN));
109 if (ret==NULL)
110 CloseHandle(hFile);
111 else *((WIN32FILE_IOWIN*)ret) = w32fiow;
113 return ret;
117 uLong ZCALLBACK win32_read_file_func (opaque, stream, buf, size)
118 voidpf opaque;
119 voidpf stream;
120 void* buf;
121 uLong size;
123 uLong ret=0;
124 HANDLE hFile = NULL;
125 if (stream!=NULL)
126 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
127 if (hFile != NULL)
128 if (!ReadFile(hFile, buf, size, &ret, NULL))
130 DWORD dwErr = GetLastError();
131 if (dwErr == ERROR_HANDLE_EOF)
132 dwErr = 0;
133 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
136 return ret;
140 uLong ZCALLBACK win32_write_file_func (opaque, stream, buf, size)
141 voidpf opaque;
142 voidpf stream;
143 const void* buf;
144 uLong size;
146 uLong ret=0;
147 HANDLE hFile = NULL;
148 if (stream!=NULL)
149 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
151 if (hFile !=NULL)
152 if (!WriteFile(hFile, buf, size, &ret, NULL))
154 DWORD dwErr = GetLastError();
155 if (dwErr == ERROR_HANDLE_EOF)
156 dwErr = 0;
157 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
160 return ret;
163 long ZCALLBACK win32_tell_file_func (opaque, stream)
164 voidpf opaque;
165 voidpf stream;
167 long ret=-1;
168 HANDLE hFile = NULL;
169 if (stream!=NULL)
170 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
171 if (hFile != NULL)
173 DWORD dwSet = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
174 if (dwSet == INVALID_SET_FILE_POINTER)
176 DWORD dwErr = GetLastError();
177 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
178 ret = -1;
180 else
181 ret=(long)dwSet;
183 return ret;
186 long ZCALLBACK win32_seek_file_func (opaque, stream, offset, origin)
187 voidpf opaque;
188 voidpf stream;
189 uLong offset;
190 int origin;
192 DWORD dwMoveMethod=0xFFFFFFFF;
193 HANDLE hFile = NULL;
195 long ret=-1;
196 if (stream!=NULL)
197 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
198 switch (origin)
200 case ZLIB_FILEFUNC_SEEK_CUR :
201 dwMoveMethod = FILE_CURRENT;
202 break;
203 case ZLIB_FILEFUNC_SEEK_END :
204 dwMoveMethod = FILE_END;
205 break;
206 case ZLIB_FILEFUNC_SEEK_SET :
207 dwMoveMethod = FILE_BEGIN;
208 break;
209 default: return -1;
212 if (hFile != NULL)
214 DWORD dwSet = SetFilePointer(hFile, offset, NULL, dwMoveMethod);
215 if (dwSet == INVALID_SET_FILE_POINTER)
217 DWORD dwErr = GetLastError();
218 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
219 ret = -1;
221 else
222 ret=0;
224 return ret;
227 int ZCALLBACK win32_close_file_func (opaque, stream)
228 voidpf opaque;
229 voidpf stream;
231 int ret=-1;
233 if (stream!=NULL)
235 HANDLE hFile;
236 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
237 if (hFile != NULL)
239 CloseHandle(hFile);
240 ret=0;
242 free(stream);
244 return ret;
247 int ZCALLBACK win32_error_file_func (opaque, stream)
248 voidpf opaque;
249 voidpf stream;
251 int ret=-1;
252 if (stream!=NULL)
254 ret = ((WIN32FILE_IOWIN*)stream) -> error;
256 return ret;
259 void fill_win32_filefunc (pzlib_filefunc_def)
260 zlib_filefunc_def* pzlib_filefunc_def;
262 pzlib_filefunc_def->zopen_file = win32_open_file_func;
263 pzlib_filefunc_def->zread_file = win32_read_file_func;
264 pzlib_filefunc_def->zwrite_file = win32_write_file_func;
265 pzlib_filefunc_def->ztell_file = win32_tell_file_func;
266 pzlib_filefunc_def->zseek_file = win32_seek_file_func;
267 pzlib_filefunc_def->zclose_file = win32_close_file_func;
268 pzlib_filefunc_def->zerror_file = win32_error_file_func;
269 pzlib_filefunc_def->opaque=NULL;